package com.mxpio.erp.common.order.policy;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.collections4.CollectionUtils;

import com.mxpio.erp.common.ModuleUtils;
import com.mxpio.erp.common.enums.CommonEnums;
import com.mxpio.erp.common.enums.NoticeStatusEnums;
import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.jpa.model.Notice;
import com.mxpio.erp.common.jpa.model.NoticeLine;
import com.mxpio.erp.common.jpa.model.Order;
import com.mxpio.erp.common.technology.entity.Item;
import com.mxpio.erp.common.vo.OrderExcuteLineVo;
import com.mxpioframework.common.util.BeanReflectionUtils;
import com.mxpioframework.jpa.JpaUtil;

/**
 * 根据策略执行订单单据
 *
 */
public abstract class AbstractBizOrderPolicy implements OrderPolicy {
	
	@Override
	public void apply(OrderContext context) {
		if(beforeExecute(context)) {
			execute(context);
			Order order = context.getEntity();
			List<OrderExcuteLineVo> orderExcuteVos = context.getOrderExcuteVos();
			
			String[] accessTypes = context.getAccessType().split(",");
			for(String accessType : accessTypes){
				// 遍历执行行 生成通知单
				Map<String,Notice> noticeMap = new HashMap<>();
				Map<String,List<OrderExcuteLineVo>> voMap = new HashMap<>();
				for(OrderExcuteLineVo orderExcuteVo : orderExcuteVos) {
					Notice notice = noticeMap.get(orderExcuteVo.getWhCode() + orderExcuteVo.getInQualityControl());
					if(notice == null) {
						if(CommonEnums.YesNo.YES.getCode().equals(orderExcuteVo.getInQualityControl())&&ModuleUtils.contains("Quality")){
							notice = (Notice) BeanReflectionUtils.newInstance(context.getQualityClass());
						}else{
							notice = (Notice) BeanReflectionUtils.newInstance(context.getNoticeClass());
						}
						
						notice.setBizOrderNo(order.getBizNo());
						notice.setNoticeDate(new Date());
						notice.setWhCode(orderExcuteVo.getWhCode());
						notice.setAccessType(accessType);
						notice.setPickingType(context.getPickingType());
						notice.setNoticeStatus(NoticeStatusEnums.NoticeStatus.CREATE.getStatusCode());
						// TODO 根据供应链策略判断是否产生申请单
						
						noticeMap.put(orderExcuteVo.getWhCode() + orderExcuteVo.getInQualityControl(), notice);
						voMap.put(orderExcuteVo.getWhCode() + orderExcuteVo.getInQualityControl(), new ArrayList<OrderExcuteLineVo>());
					}
					voMap.get(orderExcuteVo.getWhCode() + orderExcuteVo.getInQualityControl()).add(orderExcuteVo);
				}
				// 生成通知单明细
				for(Entry<String,Notice> entry : noticeMap.entrySet()) {
					Notice notice = entry.getValue();
					JpaUtil.save(notice);
					int lineNo = 0;
					for(OrderExcuteLineVo vo : voMap.get(entry.getKey())) {
						lineNo++;
						NoticeLine line = notice.createLine(vo);
						line.setLineNo(lineNo+"");
						Item item = JpaUtil.linq(Item.class).idEqual(vo.getItemCode()).findOne();
						line.setItemName(item.getItemName());
						line.setItemSpec(item.getItemSpec());
						line.setUnitCode(item.getUnitCode());
						JpaUtil.save(line);
					}
				}
			}
			if(OrderStatusEnums.OrderStatus.PLAN.getStatusCode().equals(order.getOrderStatus())||
					OrderStatusEnums.OrderStatus.ORDERED.getStatusCode().equals(order.getOrderStatus())){
				order.setOrderStatus(OrderStatusEnums.OrderStatus.PROCESSING.getStatusCode());
				JpaUtil.update(order);
			}
			afterExecute(context);
		} else {
			if(CollectionUtils.isEmpty(context.getErrors())) {
				context.addError("前置校验不通过");
			}
		}
	}
	
	public abstract void execute(OrderContext context);
	
	public abstract boolean beforeExecute(OrderContext context);

	public abstract void afterExecute(OrderContext context);

}
